/*
 * @lc app=leetcode.cn id=202 lang=java
 *
 * [202] 快乐数
 */

// @lc code=start
class Solution {
    public boolean isHappy(int n) {
        int slow = happyNum(n);
        int fast = happyNum(happyNum(n));
        //前提是没有成环，并且没有变为1
        while(slow != fast && (slow != 1 || fast !=1)){
            slow = happyNum(slow);
            fast = happyNum(happyNum(fast));
        }
        if(slow == 1 || fast == 1){
            return true;
        }else{
            return false;
        }
    }
    public int happyNum(int n){
        int happyNum = 0;

        int separate = 0;
        while(true){
            //取最后一位数
            separate = n%10;
            happyNum += separate*separate;

            //去掉最后一位数
            n = n / 10;

            if(n == 0){
                break;
            }
        }
        return happyNum;
    }
}
// @lc code=end

